.\"
.\" This file and its contents are supplied under the terms of the
.\" Common Development and Distribution License ("CDDL"), version 1.0.
.\" You may only use this file in accordance with the terms of version
.\" 1.0 of the CDDL.
.\"
.\" A full copy of the text of the CDDL should have accompanied this
.\" source.  A copy of the CDDL is also available via the Internet at
.\" http://www.illumos.org/license/CDDL.
.\"
.\"
.\" Copyright (c) 2017, Joyent, Inc.
.\" Copyright 2022 Oxide Computer Company
.\"
.Dd December 11, 2022
.Dt MC_TX 9E
.Os
.Sh NAME
.Nm mc_tx ,
.Nm mri_tx
.Nd transmit a message block chain
.Sh SYNOPSIS
.In sys/mac_provider.h
.Ft "mblk_t *"
.Fo prefix_m_tx
.Fa "void *driver"
.Fa "mblk_t *mp_chain"
.Fc
.Ft "mblk_t *"
.Fo prefix_ring_tx
.Fa "void *driver_rh"
.Fa "mblk_t *mp_chain"
.Fc
.Sh INTERFACE LEVEL
illumos DDI specific
.Pp
The
.Fn mri_tx
entry point is
.Sy Uncommitted -
API and ABI stability is not guaranteed.
.Sh PARAMETERS
.Bl -tag -width Fa
.It Fa driver
A pointer to the driver's private data that was passed in via the
.Fa m_pdata
member of the
.Xr mac_register 9S
structure to the
.Xr mac_register 9F
function.
.It Fa driver_rh
A pointer to the driver's private data for the ring that was passed in
via the
.Fa mri_driver
member of the
.Xr mac_ring_info 9S
structure.
This is initialized by the driver when its
.Xr mr_rget 9E
is called by MAC.
.It Fa mp_chain
A series of
.Xr mblk 9S
structures that may have multiple independent packets linked together on
their
.Fa b_next
member.
.El
.Sh DESCRIPTION
The
.Fn mc_tx
entry point is called when the system requires a device driver to
transmit data.
The device driver will receive a chain of message blocks.
The
.Fa mp_chain
argument represents the first frame.
The frame may be spread out across one or more
.Xr mblk 9S
structures that are linked together by the
.Fa b_cont
member.
There may be multiple frames, linked together by the
.Fa b_next
pointer of the
.Xr mblk 9S .
.Pp
For each frame, the driver should allocate the required resources and
prepare it for being transmitted on the wire.
The driver may opt to copy those resources to a DMA buffer or it may bind them.
For more information on these options, see the
.Sx MBLKS AND DMA
section of
.Xr mac 9E .
.Pp
As it processes each frame in the chain, if the device driver has
advertised either of the
.Dv MAC_CAPAB_HCKSUM
or
.Dv MAC_CAPAB_LSO
flags, it must check whether either apply for the given frame using the
.Xr mac_hcksum_get 9F
and
.Xr mac_lso_get 9F
functions respectively.
If either is enabled for the given frame, the hardware must arrange for that to
be taken care of.
.Pp
For each frame that the device driver processes it is responsible for
doing one of three things with it:
.Bl -enum
.It
Transmit the frame.
.It
Drop the frame by calling
.Xr freemsg 9F
on the individual mblk_t.
.It
Return the frames to indicate that resources are not available.
.El
.Pp
The device driver is in charge of the memory associated with
.Fa mp_chain .
If the device driver does not return the message blocks to the MAC
framework, then it must call
.Xr freemsg 9F
on the frames.
If it does not, the memory associated with them will be leaked.
When a frame is being transmitted, if the device driver performed DMA binding,
it should not free the message block until after it is guaranteed that the frame
has been transmitted.
If the message block was copied to a DMA buffer, then it is allowed to call
.Xr freemsg 9F
at any point.
.Pp
In general, the device driver should not drop frames without
transmitting them unless it has no other choice.
Times when this happens may include the device driver being in a state where it
can't transmit, an error was found in the frame while trying to establish the
checksum or LSO state, or some other kind of error that represents an issue with
the passed frame.
.Pp
The device driver should not free the chain when it does not have enough
resources.
For example, if entries in a device's descriptor ring fill up, then it should
not drop those frames and instead should return all of the frames that were not
transmitted.
This indicates to the stack that the device is full and that flow control should
be asserted.
Back pressure will be applied to the rest of the stack, allowing most systems
to behave better.
.Pp
Once a device driver has returned unprocessed frames from its
.Fn mc_tx
entry point, then the device driver will not receive any additional
calls to its
.Fn mc_tx
entry point until it calls the
.Xr mac_tx_update 9F
function to indicate that resources are available again.
Note that because it is the device driver that is calling this function to
indicate resources are available, it is very important that it only return
frames in cases where the device driver itself will be notified that resources
are available again.
For example, when it receives an interrupt indicating that the data that it
transmitted has been completed so it can use entries in its descriptor ring or
other data structures again.
.Pp
The device driver can obtain access to its soft state through the
.Fa driver
member.
It should cast it to the appropriate structure.
The device driver should employ any necessary locking to access the transmit
related data structures.
Note that the device driver should expect that it may have its transmit
endpoints called into from other threads while it's servicing device interrupts
related to them.
.Pp
The
.Fn mri_tx
entry point is similar to the
.Fn mc_tx
entry point, except that it is used by device drivers that have
negotiated the
.Dv MAC_CAPAB_RINGS
capability with transmit rings.
The driver should follow all of the same rules described earlier, except
that it will access a ring-specific data structure through
.Fa driver_rh
and when it needs to update that there is additional space available, it
must use
.Xr mac_tx_ring_update 9F
and not
.Xr mac_tx_update 9F .
.Pp
When the
.Fn mri_tx
entry point is called, the ring that should be used has been specified.
The driver must not attempt to use any other ring than the one specified
by
.Fa driver_rh
for any reason, including a lack of resources or an attempt to perform
its own hashing.
.Sh CONTEXT
The
.Fn mc_tx
entry point may be called from
.Sy kernel
or
.Sy interrupt
context.
.Sh RETURN VALUES
Upon successful completion, the device driver should return
.Dv NULL .
Otherwise, it should return all unprocessed message blocks and ensure
that it calls either
.Xr mac_tx_update 9F
or
.Xr mac_tx_ring_update 9F
some time in the future.
.Sh SEE ALSO
.Xr mac 9E ,
.Xr mac_capab_rings 9E ,
.Xr mr_rget 9E ,
.Xr freemsg 9F ,
.Xr mac_hcksum_get 9F ,
.Xr mac_lso_get 9F ,
.Xr mac_register 9F ,
.Xr mac_tx_ring_update 9F ,
.Xr mac_tx_update 9F ,
.Xr mac_register 9S ,
.Xr mac_ring_info 9S ,
.Xr mblk 9S
